package com.wjw.service;

import com.wjw.mapper.EmployeeTableMapper;
import com.wjw.model.Employee_table;
import com.wjw.utils.GetSqlSessionUtils;
import com.wjw.viewmodel.Employee_table_view;
import org.apache.ibatis.session.SqlSession;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 职工表数据库操作:逻辑控制层
 */
@Service
public class EmployeeTableService {

//    private SqlSession sqlsession= GetSqlSessionUtils.getSqlSession();
//    private EmployeeTableMapper mapper=sqlsession.getMapper(EmployeeTableMapper.class);

    public boolean login(Employee_table_view employee_table_view){
        SqlSession sqlsession= GetSqlSessionUtils.getSqlSession();
        EmployeeTableMapper mapper=sqlsession.getMapper(EmployeeTableMapper.class);
        List<Employee_table> all = mapper.findAll();
        for(Employee_table e:all){
            if(e.getEmployee_id().equals(employee_table_view.getEmployee_id())&&e.getEmployee_pwd().equals(employee_table_view.getEmployee_pwd())){
                return true;
            }
        }
        sqlsession.close();
        return false;
    }

    public List<Employee_table_view> findByPage(int page,int limit){
        SqlSession sqlsession= GetSqlSessionUtils.getSqlSession();
        EmployeeTableMapper mapper=sqlsession.getMapper(EmployeeTableMapper.class);
        try {
            int start=(page-1)*limit;
            List<Employee_table> all = mapper.findByPage(start,limit);
            List<Employee_table_view> list=new ArrayList<Employee_table_view>();
            for(Employee_table e:all){
                Employee_table_view employee = new Employee_table_view();
                employee.setEmployee_age(e.getEmployee_age());
                employee.setEmployee_birthday(e.getEmployee_birthday());
                employee.setEmployee_entryday(e.getEmployee_entryday());
                employee.setEmployee_pwd(e.getEmployee_pwd());
                employee.setEmployee_id(e.getEmployee_id());
                employee.setEmployee_sex(e.getEmployee_sex());
                employee.setEmployee_name(e.getEmployee_name());
                employee.setEmployee_type_name(mapper.getEmployeeTypeName(e.getEmployee_type_id()).getEmployee_type_name());
                list.add(employee);
            }
            return list;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }finally {
            sqlsession.close();
        }
    }


    /**
     * 职工信息浏览
     * @return
     */
    public List<Employee_table_view> findAll(){
        SqlSession sqlsession= GetSqlSessionUtils.getSqlSession();
        EmployeeTableMapper mapper=sqlsession.getMapper(EmployeeTableMapper.class);
        try {
            List<Employee_table> all = mapper.findAll();
            List<Employee_table_view> list=new ArrayList<Employee_table_view>();
            for(Employee_table e:all){
                Employee_table_view employee = new Employee_table_view();
                employee.setEmployee_age(e.getEmployee_age());
                employee.setEmployee_birthday(e.getEmployee_birthday());
                employee.setEmployee_entryday(e.getEmployee_entryday());
                employee.setEmployee_pwd(e.getEmployee_pwd());
                employee.setEmployee_id(e.getEmployee_id());
                employee.setEmployee_sex(e.getEmployee_sex());
                employee.setEmployee_name(e.getEmployee_name());
                employee.setEmployee_type_name(mapper.getEmployeeTypeName(e.getEmployee_type_id()).getEmployee_type_name());
                list.add(employee);
            }
            return list;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }finally {
            sqlsession.close();
        }
    }

    /**
     * 职工信息添加
     */
    public boolean add(Employee_table_view e){
        SqlSession sqlsession= GetSqlSessionUtils.getSqlSession();
        EmployeeTableMapper mapper=sqlsession.getMapper(EmployeeTableMapper.class);
        Employee_table employee = new Employee_table();
        employee.setEmployee_age(e.getEmployee_age());
        employee.setEmployee_birthday(e.getEmployee_birthday());
        employee.setEmployee_entryday(e.getEmployee_entryday());
        employee.setEmployee_pwd(e.getEmployee_pwd());
        employee.setEmployee_id(e.getEmployee_id());
        employee.setEmployee_sex(e.getEmployee_sex());
        employee.setEmployee_name(e.getEmployee_name());
        employee.setEmployee_type_id(mapper.getEmployeeTypeId(e.getEmployee_type_name()).getEmployee_type_id());
        try {
            mapper.add(employee);
            sqlsession.commit();
            return true;
        } catch (Exception e1) {
            e1.printStackTrace();
            return false;
        }finally {
            sqlsession.close();
        }
    }

    /**
     * 职工信息删除
     * @param id
     */
    public boolean delete(String id){
        SqlSession sqlsession= GetSqlSessionUtils.getSqlSession();
        EmployeeTableMapper mapper=sqlsession.getMapper(EmployeeTableMapper.class);
        try {
            mapper.delete(id);
            sqlsession.commit();
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }finally {
            sqlsession.close();
        }
    }

    /**
     * 职工信息修改
     */
    public boolean change(Employee_table_view e){
        SqlSession sqlsession= GetSqlSessionUtils.getSqlSession();
        EmployeeTableMapper mapper=sqlsession.getMapper(EmployeeTableMapper.class);
        try {
            Employee_table employee = new Employee_table();
            employee.setEmployee_age(e.getEmployee_age());
            employee.setEmployee_birthday(e.getEmployee_birthday());
            employee.setEmployee_entryday(e.getEmployee_entryday());
            employee.setEmployee_pwd(e.getEmployee_pwd());
            employee.setEmployee_id(e.getEmployee_id());
            employee.setEmployee_sex(e.getEmployee_sex());
            employee.setEmployee_name(e.getEmployee_name());
            employee.setEmployee_type_id(mapper.getEmployeeTypeId(e.getEmployee_type_name()).getEmployee_type_id());
            mapper.change(employee);
            sqlsession.commit();
            return true;
        } catch (Exception e1) {
            e1.printStackTrace();
            return false;
        }finally {
            sqlsession.close();
        }
    }

}
